﻿#include "klb_sdk.h"
#include "klua/klua.h"
#include <assert.h>


static int lua_klbsdk_request(lua_State* L)
{
    const char* p_request = luaL_checkstring(L, 1);
    
    char* p_response = NULL;
    int ret = klbsdk_request(p_request, &p_response);

    if (p_response)
    {
        lua_pushstring(L, p_response);
        klbsdk_free(p_response);
    }
    else
    {
        lua_pushstring(L, "");
    }

    lua_pushinteger(L, ret);
    return 2;
}

KLB_API int luaopen_klb_sdk(lua_State* L)
{
    static luaL_Reg lib[] =
    {
        { "request",    lua_klbsdk_request },

        { NULL,         NULL }
    };

    // 创建导出库函数
    luaL_newlib(L, lib);

    return 1;
}
